package com.qing.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.cmsservice.entity.pojo.CrmBanner;
import com.qing.cmsservice.mapper.CrmBannerMapper;
import com.qing.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.servicebase.exception.JsonException;
import com.qing.servicebase.utils.ResponseResult;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-14
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Cacheable(value = "banner", key = "'selectIndexList'")
    @Override
    public ResponseResult selectIndexList() {
        List<CrmBanner> list = baseMapper.selectList(new
                QueryWrapper<CrmBanner>().orderByDesc("sort"));
        return list!=null?ResponseResult.ofSuccess(list):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }

    @Override
    public ResponseResult pageBanner(Page<CrmBanner> pageParam, Object o) {
        IPage<CrmBanner> crmBannerIPage = baseMapper.selectPage(pageParam, null);
        return crmBannerIPage!=null?ResponseResult.ofSuccess(crmBannerIPage):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }

    @Override
    public ResponseResult getBannerById(String id) {
        CrmBanner crmBanner = baseMapper.selectById(id);
        return crmBanner!=null?ResponseResult.ofSuccess(crmBanner):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public ResponseResult saveBanner(CrmBanner banner) {
        int insert = baseMapper.insert(banner);
        return insert>0?ResponseResult.ofSuccess(null):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public ResponseResult updateBannerById(CrmBanner banner) {
        int result = baseMapper.updateById(banner);
        return result>0?ResponseResult.ofSuccess(null):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public ResponseResult removeBannerById(String id) {
        int result = baseMapper.deleteById(id);
        return result>0?ResponseResult.ofSuccess(null):
                ResponseResult.ofException(new JsonException(201,"操作失败"));
    }

}
